//
//  second2ViewController.m
//  豆瓣综合网页
//
//  Created by dllo on 16/1/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "second2ViewController.h"
#import "Model1.h"
#import "secondTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MyCollectionViewCell.h"
@interface second2ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *arrModel;
@end

@implementation second2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
    self.navigationItem.title = @"电影";
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((375 - 40)/3, 150);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    [collectionView release];
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    UIImage *image = [UIImage imageNamed:@"rili.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self dataHandler];

    
}
//跳转
- (void)itemAction:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Model1 *model = _arrModel[indexPath.row];
    cell.titleLabel.text = model.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.images[@"large"]]];
    
    
    return cell;
}

- (void)dataHandler {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        Model1 *model = [Model1 model];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
